package com.travel.server.manager.impl.command.user;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.user.DeleteUserRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.UserDal;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 12:57
 * @Desc:
 */
@Component
public class DeleteUserCommand extends AbstractCommand<DeleteUserRequest, EmptyResponse> {

    @Autowired
    private UserDal userDal;

    @Override
    protected void checkInput(DeleteUserRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteUserRequest request) {
        checkInput(request);
        UserInfo user = getUserInfo();

        UserInfo userInfo = userDal.selectById(request.getId());

        userInfo.setState(CommonConstant.NO);
        userInfo.setUpdateBy(user.getName());
        userInfo.setUpdateTime(new Date());

        userDal.insertOrUpdate(userInfo);
        return new EmptyResponse();
    }
}